<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="农事操作时间" prop="jobTime">
        <el-date-picker clearable
          v-model="queryParams.jobTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择农事操作时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="地块编号" prop="plotId">
        <el-input
          v-model="queryParams.plotId"
          placeholder="请输入地块编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="天气状况" prop="jobWeather">
        <el-input
          v-model="queryParams.jobWeather"
          placeholder="请输入天气状况"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物候期" prop="phenName">
        <el-select v-model="queryParams.phenName" placeholder="请选择物候期" clearable>
          <el-option
            v-for="dict in grape_phen_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="农事类型" prop="agrType">
        <el-select v-model="queryParams.agrType" placeholder="请选择农事类型" clearable>
          <el-option
            v-for="dict in grape_arg_sort"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="作业品种" prop="grapeSort">
        <el-input
          v-model="queryParams.grapeSort"
          placeholder="请输入作业品种"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="作业面积" prop="jobArea">
        <el-input
          v-model="queryParams.jobArea"
          placeholder="请输入作业面积"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="农业投入品" prop="agrInput">
        <el-input
          v-model="queryParams.agrInput"
          placeholder="请输入农业投入品"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['grape:job:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['grape:job:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['grape:job:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['grape:job:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jobList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="农事管理编号" align="center" prop="jobId" />
      <el-table-column label="农事操作时间" align="center" prop="jobTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.jobTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地块编号" align="center" prop="plotId" />
      <el-table-column label="天气状况" align="center" prop="jobWeather" />
      <el-table-column label="物候期" align="center" prop="phenName">
        <template #default="scope">
          <dict-tag :options="grape_phen_name" :value="scope.row.phenName"/>
        </template>
      </el-table-column>
      <el-table-column label="农事类型" align="center" prop="agrType">
        <template #default="scope">
          <dict-tag :options="grape_arg_sort" :value="scope.row.agrType"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="作业品种" align="center" prop="grapeSort" /> -->
      <el-table-column label="作业面积" align="center" prop="jobArea" />
      <el-table-column label="农业投入品" align="center" prop="agrInput" />
      <el-table-column label="作业人员" align="center" prop="jobMan">
        <template #default="scope">
          <dict-tag :options="grape_job_man" :value="scope.row.jobMan ? scope.row.jobMan.split(',') : []"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="jobNote" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['grape:job:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['grape:job:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改农事管理信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="jobRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="农事操作时间" prop="jobTime">
          <el-date-picker clearable
            v-model="form.jobTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择农事操作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地块编号" prop="plotId">
          <el-checkbox-group v-model="form.plotId">
            <el-checkbox
              v-for="plot in plotList"
              :key="plot.plotId"
              :label="plot.plotId">
              {{plot.plotId}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="天气状况" prop="jobWeather">
          <el-input v-model="form.jobWeather" placeholder="请输入天气状况" />
        </el-form-item>
        <el-form-item label="物候期" prop="phenName">
          <el-select v-model="form.phenName" placeholder="请选择物候期">
            <el-option
              v-for="dict in grape_phen_name"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="农事类型" prop="agrType">
          <el-select v-model="form.agrType" placeholder="请选择农事类型">
            <el-option
              v-for="dict in grape_arg_sort"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="作业品种" prop="grapeSort">
          <el-input v-model="form.grapeSort" placeholder="请输入作业品种" />
        </el-form-item> -->
        <el-form-item label="作业面积" prop="jobArea">
          <el-input v-model="form.jobArea" placeholder="请输入作业面积" />
        </el-form-item>
        <el-form-item label="农业投入品" prop="agrInput">
          <el-input v-model="form.agrInput" placeholder="请输入农业投入品" />
        </el-form-item>
        <el-form-item label="作业人员" prop="jobMan">
          <el-checkbox-group v-model="form.jobMan">
            <el-checkbox
              v-for="dict in grape_job_man"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="备注" prop="jobNote">
          <el-input v-model="form.jobNote" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Job">
import { listJob, getJob, delJob, addJob, updateJob } from "@/api/grape/job";

const { proxy } = getCurrentInstance();
const { grape_phen_name, grape_arg_sort, grape_job_man } = proxy.useDict('grape_phen_name', 'grape_arg_sort', 'grape_job_man');

const jobList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    jobTime: null,
    plotId: null,
    jobWeather: null,
    phenName: null,
    agrType: null,
    // grapeSort: null,
    jobArea: null,
    agrInput: null,
    jobMan: null,
    jobNote: null
  },
  rules: {
    jobTime: [
      { required: true, message: "农事操作时间不能为空", trigger: "blur" }
    ],
    plotId: [
      { required: true, message: "地块编号不能为空", trigger: "blur" }
    ],
    jobWeather: [
      { required: true, message: "天气状况不能为空", trigger: "blur" }
    ],
    phenName: [
      { required: true, message: "物候期不能为空", trigger: "change" }
    ],
    agrType: [
      { required: true, message: "农事类型不能为空", trigger: "change" }
    ],
    grapeSort: [
      { required: true, message: "作业品种不能为空", trigger: "blur" }
    ],
    jobArea: [
      { required: true, message: "作业面积不能为空", trigger: "blur" }
    ],
    agrInput: [
      { required: true, message: "农业投入品不能为空", trigger: "blur" }
    ],
    jobMan: [
      { required: true, message: "作业人员不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询农事管理信息列表 */
function getList() {
  loading.value = true;
  listJob(queryParams.value).then(response => {
    jobList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 查询地块基本信息列表,外键 */
import {listPlot} from "@/api/grape/plot";
const plotList = ref([]);
function getPlotList() {
  listPlot("").then(response => {
    plotList.value = response.rows;
    plotList.value.forEach(plot => {
      console.log(plot)
      plot.plotId = plot.plotId.toString()
    });
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    jobId: null,
    jobTime: null,
    plotId: [],
    jobWeather: null,
    phenName: null,
    agrType: null,
    grapeSort: null,
    jobArea: null,
    agrInput: null,
    jobMan: [],
    jobNote: null
  };
  proxy.resetForm("jobRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.jobId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加农事管理信息";
  getPlotList();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _jobId = row.jobId || ids.value
  getJob(_jobId).then(response => {
    form.value = response.data;
    form.value.jobMan = form.value.jobMan.split(",");
    form.value.plotId = form.value.plotId.split(",");
    open.value = true;
    title.value = "修改农事管理信息";
  });
  getPlotList();
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["jobRef"].validate(valid => {
    if (valid) {
      form.value.jobMan = form.value.jobMan.join(",");
      form.value.plotId = form.value.plotId.join(",");
      if (form.value.jobId != null) {
        updateJob(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addJob(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _jobIds = row.jobId || ids.value;
  proxy.$modal.confirm('是否确认删除农事管理信息编号为"' + _jobIds + '"的数据项？').then(function() {
    return delJob(_jobIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('grape/job/export', {
    ...queryParams.value
  }, `job_${new Date().getTime()}.xlsx`)
}

getList();
</script>
